Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
HTMLElement.innerHTML adding attributes to </span> closing tag, and I don't know why

I am using Angular 13 with a pipe to reformat text in a contenteditable div. Everything is working fine until the innerHtml is replaced with the new code. The attributes from the last child node (a span tag) are appended to the inside of the closing span tag. I have no idea why this is happening.

I'm using TypeScript, with Angular 13, on Chromium (MS Edge) v97.0.1072.76 and Firefox v95.0.

Here's an example:

reformatContent(event: Event) {
const el = (event.target as HTMLElement);

const originalHtml = el.innerHTML;
console.log(originalHtml); // "Some text <span id="someTextId" class="someTextClass">More Text</span>

const newHtml = this.reformatText(originalHtml);
console.log(newHtml); // "Some text <span id="someTextId" class="someTextClass someNewClass">More Text</span> <-- this is ok, what I expected.

el.innerHTML= newHtml;
console.log(el.innerHTML); // "Some text <span id="someTextId" class="someTextClass someNewClass">More Text</span id="someTextId" class="someTextClass someNewClass"> <-- why is this happening???
}

I just want to be clear that the originalHtml includes the close </span> tag. So I don't think the browser is trying to close the tag.

Edit for typo.

Edit 2: I updated the original code for typos and to make it more clear how I was getting the innerHTML. I didn't think that was where the problem was, but apparently that's it, because this works:

reformatContent(event: Event) {
const e = (event.target as HTMLElement);
const el = document.getElementById(e.id);

const originalHtml = el.innerHTML;
console.log(originalHtml); // "Some text <span id="someTextId" class="someTextClass">More Text</span>

const newHtml = this.reformatText(originalHtml);
console.log(newHtml); // "Some text <span id="someTextId" class="someTextClass someNewClass">More Text</span> <-- this is ok, what I expected.

el.innerHTML= newHtml;
console.log(el.innerHTML); // "Some text <span id="someTextId" class="someTextClass someNewClass">More Text</span> <-- now it works
}

I don't understand why the original way of getting the element caused this problem. I fixed it, but I'd really like to understand what's happening here.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

What is reformatText function?

this.reformatText(originalHtml)

This function is changing your span and your bug is from here.

You might say that your result are right but you are using console.log on an invalid variable (originalHtml)

What is and where is originalHtml?

But generally what I know, your problem is because of this line:

const newHtml = this.reformatText(originalHtml);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda